﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Persister;
using Kaidou.Model;
using System.Collections.Generic;
using Kaidou.Util;
using Kaidou.Bll;


public partial class master_master_trader_detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];

        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }
        else if (!loginModel.isOperator() && !loginModel.isJimukyoku())
        {
            Response.Redirect("../menu.aspx");
        }

        if (loginModel != null && loginModel.isOperator())
        {
            updateButton.Visible = true;
            deleteButton.Visible = true;
        }

        if (!IsPostBack)
        {
            string area = Resources.Resource.area;
            string[] areaArray = area.Split(',');
            for (int i = 0; i < areaArray.Length; i++)
            {
                ListItem item = new ListItem();
                item.Value = (i + 1) + "";
                item.Text = areaArray[i];
                trader_pref_code.Items.Insert(i + 1, item);
            }

            MST_TraderPersister dao1 = new MST_TraderPersister();
            MST_UnionPersister dao2 = new MST_UnionPersister();
            MST_Union_BranchPersister dao3 = new MST_Union_BranchPersister();

            MstTrader model = dao1.getDetail(int.Parse(Request.QueryString["seqno"].ToString()));
            Session["trader"] = model;

            union_code.Text = model.UnionCode;
            union_code_name.Text=dao2.getDetail(model.UnionCode).OfficeName;
            branch_code.Text = StringUtil.getBranchCodeNumberFormat( model.BranchCode);
            if (model.BranchCode == 0)
            {
                branch_code_name.Text = "本店";
            }
            else
            {
                branch_code_name.Text = dao3.getDetail(model.UnionCode, model.BranchCode).officename;
            }
            if (model.TraderRegNo != null && model.TraderRegNo.Length > 0)
            {
                trader_reg_no1.Text = model.TraderRegNo.Substring(0, 2);
                trader_reg_no2.Text = model.TraderRegNo.Substring(2, 5);
            }
            trader_name.Text = model.TraderName;
            trader_kana.Text = model.TraderKana;
            if (model.TraderZipNo != null && model.TraderZipNo.Length > 0)
            {
                trader_zip_no1.Text = model.TraderZipNo.Substring(0, 3);
                trader_zip_no2.Text = model.TraderZipNo.Substring(3, 4);
            }
            trader_pref_code.SelectedIndex = model.TraderPrefCode;
            trader_city.Text = model.TraderCity;
            trader_address.Text = model.TraderAddress;
            trader_tel.Text = model.TraderTel;
            daihyo_name.Text = model.DaihyoName;
            daihyo_kana.Text = model.DaihyoKana;
        }
    }

    protected void delete(object sender, EventArgs e)
    {
        MstTrader M = (MstTrader)Session["trader"];

        M.UpdDate = System.DateTime.Now;
        M.UpdUnionCode = (string)Session["Login_Union_code"];
        M.UpdBranchCode = (short)Session["Login_Branch_code"];
        M.UpdFuncId = "deleteTrader()";
        M.TraderSts = MstTrader.STATUS_DELETE;

        Session["trader"] = M;
        Response.Redirect("master_trader_delete_conf.aspx");


    }

    protected void update(object sender, EventArgs e)
    {

        //カプセル化オブジェクト
        MstTrader model = (MstTrader)Session["trader"];
        model.UnionCode = union_code.Text;
        if (!ParameterCheckUtility.isNumber(branch_code.Text.Trim()))
        {
            model.BranchCode = -1;
        }
        else
        {
            model.BranchCode = branch_code.Text.Trim().Length == 0 ? (short)(-1) : short.Parse(branch_code.Text);
        }
        model.TraderRegNo = trader_reg_no1.Text + trader_reg_no2.Text;
        model.TraderRegNo1 = trader_reg_no1.Text;
        model.TraderRegNo2 = trader_reg_no2.Text;
        model.TraderName = trader_name.Text;
        model.TraderKana = trader_kana.Text;
        model.TraderZipNo = trader_zip_no1.Text + trader_zip_no2.Text;
        model.TraderZipNo1 = trader_zip_no1.Text;
        model.TraderZipNo2 = trader_zip_no2.Text;
        model.TraderPrefCode = short.Parse(trader_pref_code.Text);
        model.TraderCity = trader_city.Text;
        model.TraderAddress = trader_address.Text;
        model.TraderTel = trader_tel.Text;
        model.DaihyoName = daihyo_name.Text;
        model.DaihyoKana = daihyo_kana.Text;

        model.UpdDate = System.DateTime.Now;
        model.UpdUnionCode = (string)Session["Login_Union_code"];
        model.UpdBranchCode = (short)Session["Login_Branch_code"];
        model.UpdFuncId = "updateMasterUnion()";

        //検証
        MST_TraderManager validation = new MST_TraderManager();
        List<string> errorCodeList = validation.checkInsertOrUpdateTrader(model, Resources.Resource.ResourceManager, false);
        msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList);

        if (errorCodeList.Count == 0)
        {
            Session["trader"] = model;
            Response.Redirect("master_trader_update_conf.aspx");
        }
    }

    protected void goback(object sender, EventArgs e)
    {
        Session["back"] = 1;
        Response.Redirect("master_trader_search.aspx");

    }

}
